package com.ruoyi.web.controller.student_club.admin_controller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.student_club.domain.FirstPoster;
import com.ruoyi.student_club.utils.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.student_club.service.IFirstPosterService;
import com.ruoyi.common.core.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页轮播图Controller
 *
 * @author 无尤
 * @date 2022-01-14
 */
@Controller
@RequestMapping("/firstPoster/poster")
public class FirstPosterController extends BaseController {

    private String prefix = "firstPoster/poster";

    @Resource
    private IFirstPosterService posterService;


    @GetMapping()
    public String poster() {
        return prefix + "/banner";
    }

    @ResponseBody
    @PostMapping("/save")
    public AjaxResult save(@RequestBody List<String> fileList) {
        List<FirstPoster> posterList = fileList.stream().map(f -> new FirstPoster().setPath(f)).collect(Collectors.toList());
        posterService.saveBatch(posterList);
        return AjaxResult.success();
    }


    @ResponseBody
    @DeleteMapping("/{id:\\d+}")
    public AjaxResult remove(@PathVariable("id") Integer id) {
        FirstPoster poster = posterService.getById(id);
        UploadUtil.deletePicture(poster.getPath());
        posterService.removeById(id);
        return AjaxResult.success();
    }

    /**
     * 展示轮播图
     * @return 图列表
     */
    @ResponseBody
    @GetMapping("/showPoster")
    public AjaxResult viewPoster(){
        List list = posterService.getPoster();
        return AjaxResult.success(list);
    }

    /**
     * 上传轮播图到首页
     * @param file 文件
     * @return 返回存储的路径
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public AjaxResult upload(MultipartFile file){
        String typeName = "firstPoster";
        String url = UploadUtil.upload(file, typeName);
        return AjaxResult.success("上传成功",url);
    }

}
